package com.mark.mall.web.platform.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mark.mall.common.myException.MyException;
import com.mark.mall.common.resultBean.R;
import com.mark.mall.web.platform.dto.param.MakeOrderParam;
import com.mark.mall.web.platform.entity.*;
import com.mark.mall.web.platform.mapper.*;
import com.mark.mall.web.platform.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 销售单表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-03-03
 */
@Service
@RequiredArgsConstructor
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    private final MemberMapper memberMapper;
    private final CartMapper cartMapper;
    private final ProductMapper productMapper;
    private final OrderDetailMapper orderDetailMapper;

    @Override
    public R<Boolean> makeOrder(MakeOrderParam param) {
        Member member = memberMapper.selectById(param.getMemberId());
        if (member == null) {
            throw new MyException("id为{" + param.getMemberId() + "}的会员不存在！");
        }
        // 前端传入购物车ids
        List<String> cartIds = param.getList();
        Order order = new Order();
        order.setMemberid(member.getId());
        String time = DateUtil.now();
        // 订单编号规则 年-月-日-时-分-00-四位随机数
        order.setOrderno(time.substring(0, 4) + time.substring(5, 7) + time.substring(8, 10) + time.substring(11, 13) + time.substring(14, 16) + "00" + (int) ((Math.random() * 9 + 1) * 1000));
        // 生成销售单，产生id，以便销售单详情关联销售单id
        baseMapper.insert(order);
        // 设置默认值，使用BigDecimal保证精度不缺失
        BigDecimal oPrice = new BigDecimal("0.00");
        if (cartIds != null && cartIds.size() > 0) {
            for (String cartId : cartIds) {
                Cart cart = cartMapper.selectById(cartId);
                if (cart == null) continue;
                Product product = productMapper.selectById(cart.getProductId());
                if (product == null) continue;
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setNum(cart.getNum());
                // 销售单详情关联销售单
                orderDetail.setOrderid(order.getId());
                orderDetail.setPrice(product.getPrice().multiply(new BigDecimal(cart.getNum())).setScale(2, BigDecimal.ROUND_HALF_UP));
                oPrice = oPrice.add(product.getPrice().multiply(new BigDecimal(cart.getNum())).setScale(2, BigDecimal.ROUND_HALF_UP));
                orderDetail.setProductid(product.getId());
                orderDetailMapper.insert(orderDetail);
                // 销售单详情生成后删除购物车数据（真删）
                cartMapper.deleteById(cart.getId());
            }
        }
        // 将销售单总价赋值销售单
        order.setOprice(oPrice);
        baseMapper.alwaysUpdateSomeColumnById(order);
        return new R<>(true, "购买成功！");
    }
}
